package edu.ccut.saturn.studio.export.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.saturndata.model.DataItem;
import edu.ccut.saturn.studio.saturndata.model.SaturnData;
import edu.ccut.saturn.studio.saturndata.model.SaturnDataModel;

/**
 * 
 * 生成数据项构件的具体实现类
 * 
 * @author 杨艳斌
 * 
 * @version 1.1 2009.12.11
 */

public class Saturndata {
	
	private static String filename;
	private static String info;
	
	/**
	 * 创建数据项构件的html文档
	 * 
	 * @param file,filePath
	 * @return String
	 * @throws XMLException
	 */
	public static String createComponentHtml(File file,String filePath) throws XMLException{
		
		filename = file.getName().replace(".", "-");
		Document doc = SaturnW3CUtil.getDocument(file.getAbsolutePath());
		SaturnDataModel data = new SaturnDataModel(doc);
		SaturnData datas = data.getSaturnData();
		
		generateInfo(datas, filePath);
		
		return info;
	}
	
	/**
	 * 创建数据项构件的html文档的具体实现方法
	 * 
	 * @param data,filePath
	 * @return 
	 */
	private static void generateInfo(SaturnData data, String filePath) {
		
		Map<String, List<?>> parameters = new HashMap<String, List<?>>();
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		List<DataItem> dataItems = data.getDataItemList();
		String tempalatePath = "template/export/saturndata";
		Parameters para = new Parameters();
		String name = filename.split("-")[0];
		info = data.getDescription();
		
		para.setName(name);
		para.setClassname(data.getSpace());
		para.setDescription(data.getDescription());
		
		basicvaluse.add(para);
		parameters.put("parameters", dataItems);
		parameters.put("basicvaluse", basicvaluse);
		
		FreemarkerUtils.writeTemplateToFile(tempalatePath, parameters, filePath 
				+ File.separator + filename + ".html");
	}
	
}
